package com.zeny.online.config;

import com.zeny.online.job.CleanImgJob;
import org.quartz.spi.JobFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

/**
 * @ClassName QuzrtzConfig
 * @Description TODO
 * @Author zeny
 * @Date 2020/5/5 0005 11:11
 */
@Configuration
public class QuzrtzConfig {

    /**
     * @Description 创建job对象
     * @param
     * @return org.springframework.scheduling.quartz.JobDetailFactoryBean
     **/
    @Bean
    public JobDetailFactoryBean getJobDetailFactoryBean() {
        JobDetailFactoryBean factory = new JobDetailFactoryBean();
        //把我们需要定时的类放进来，MyJob就是我们自定义的定时类
        factory.setJobClass(CleanImgJob.class);
        return factory;
    }

    /**
     * @Description 创建Trigger触发器
     * @param jobDetailFactoryBean
     * @return org.springframework.scheduling.quartz.CronTriggerFactoryBean
     **/
    @Bean
    public CronTriggerFactoryBean getSimpleTriggerFactoryBean(JobDetailFactoryBean jobDetailFactoryBean) {
        CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
        //关联JobDetail对象
        cronTriggerFactoryBean.setJobDetail(jobDetailFactoryBean.getObject());
        cronTriggerFactoryBean.setCronExpression("0 * * * * ?");
        return cronTriggerFactoryBean;
    }

    /**
     * @Description 创建Scheduler对象
     * @param cronTriggerFactoryBean
     * @param jobFactory
     * @return org.springframework.scheduling.quartz.SchedulerFactoryBean
     **/
    @Bean
    public SchedulerFactoryBean getSchedulerFactoryBean(CronTriggerFactoryBean cronTriggerFactoryBean, JobFactory jobFactory) {
        SchedulerFactoryBean factory = new SchedulerFactoryBean();
        //关联trigger
        factory.setTriggers(cronTriggerFactoryBean.getObject());
        //重新JobFactory
        factory.setJobFactory(jobFactory);
        return factory;
    }
}
